package myservlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.MessageProcessing;
import dao.UserPaging;

import bean.DemoBean;
import bean.UserBean;

public class DemoController extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String pick = request.getParameter("action");

		try {
			if (pick.endsWith("message")) {
				message(request, response);
			}
			if (pick.contains("DisplayUserPaging")) {
				DisplayUserPaging(request, response);
			}
		} catch (Throwable theException) {
			System.out.println(theException);
		}

	}

	private void DisplayUserPaging(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			int page = 1;
			int recordsPerPage = 5;
			if (request.getParameter("page") != null) {
				page = Integer.parseInt(request.getParameter("page"));
			}
				UserPaging up = new UserPaging();
				List<UserBean> list = up.UserList((page-1)*recordsPerPage,recordsPerPage);
				int noOfRecords = up.getNoOfRecords();
				System.out.println("number of records = "+noOfRecords);
				int noOfPages = (int) Math.ceil(noOfRecords * 1.0/ recordsPerPage);
				System.out.println("number of page = "+noOfPages);

				request.setAttribute("userList", list);
				
				request.setAttribute("noOfPages", noOfPages);
		        request.setAttribute("currentPage", page);
		        //RequestDispatcher rd = getServletContext().getRequestDispatcher("/message/index.jsp");
		        RequestDispatcher rd = request.getRequestDispatcher("/user/UserInPaging.jsp");
				rd.forward(request, response);
			
		} catch (Throwable theException) {
			System.out.println(theException);
		}
	}

	private void message(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		try {
			DemoBean bean = new DemoBean();
			// get data from texts and store in bean
			bean.setName(request.getParameter("ft_author"));
			bean.setMail(request.getParameter("ft_email"));
			bean.setMessage(request.getParameter("ft_message"));

			// do something with action MessageProcessing;
			MessageProcessing mp = new MessageProcessing();
			boolean checkInsertDB = mp.StoreInDB(bean);

			if (checkInsertDB == true) {
				RequestDispatcher rd = getServletContext()
						.getRequestDispatcher("/message/index.jsp");
				rd.forward(request, response);

			}

			RequestDispatcher rd = getServletContext().getRequestDispatcher(
					"/message/error.jsp");
			rd.forward(request, response);

		} catch (Throwable theException) {
			System.out.println(theException);
		}
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request, response);

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request, response);

	}
}
